package com.jvmoutofmemory.platform.pushutil.dto;

import com.alibaba.fastjson2.JSON;
import com.jvmoutofmemory.platform.pushutil.constants.PushJobMethod;
import com.jvmoutofmemory.platform.pushutil.constants.PushJobParamType;
import com.jvmoutofmemory.platform.pushutil.entity.PushJob;
import com.jvmoutofmemory.platform.pushutil.response.PushResponseListener;
import org.slf4j.MDC;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Adam on 2017/6/10.
 */
public class PushJobBuilder {

    private PushJob pushJob;
    private Map<String, Object> map;

    public PushJobBuilder() {
        this.pushJob = new PushJob();
        map = new HashMap<>();
        pushJob.setStatus((short) 0);
        pushJob.setTimes(0);
        pushJob.setMsg("");
        pushJob.setCreateTime(new Date());
        pushJob.setUpdateTime(new Date());
        pushJob.setRequestId(MDC.get("rid"));
        pushJob.setMaxRetry(3);
    }

    public PushJobBuilder externalInfo(String externalInfo) {
        pushJob.setExternalInfo(externalInfo);
        return this;
    }

    public PushJobBuilder sysType(String sysTye) {
        pushJob.setSysType(sysTye);
        return this;
    }

    public PushJobBuilder url(String url) {
        pushJob.setUrl(url);
        return this;
    }

    public PushJobBuilder param(String param) {
        pushJob.setParam(param);
        return this;
    }

    public PushJobBuilder paramObj(Object param) {
        pushJob.setParam(JSON.toJSONString(param));
        return this;
    }

    public PushJobBuilder paramType(PushJobParamType paramType) {
        pushJob.setParamType(paramType.getCode());
        return this;
    }

    public PushJobBuilder method(PushJobMethod method) {
        pushJob.setMethod(method.getCode());
        return this;
    }

    public PushJobBuilder listener(PushResponseListener listener) {
        pushJob.setListener(listener.getClass().getName());
        return this;
    }

    public PushJobBuilder listenerClass(Class listener) {
        pushJob.setListener(listener.getName());
        return this;
    }

    public PushJobBuilder retry(int retryTimes) {
        pushJob.setMaxRetry(retryTimes);
        return this;
    }

    public PushJobBuilder callBackParam(Object callBackParam) {
    	if(callBackParam != null) {
    		pushJob.setCallBackParam(JSON.toJSONString(callBackParam));
    		pushJob.setCallBackType(callBackParam.getClass().getName());
    	}
        return this;
    }

    public PushJobBuilder callBackMapParam(String key, Object value) {
        map.put(key, value);
        return this;
    }

    public PushJob build() {
        if (!CollectionUtils.isEmpty(map)) {
            pushJob.setCallBackType(Map.class.getName());
            pushJob.setCallBackParam(JSON.toJSONString(map));
        }
        return pushJob;
    }

    public static PushJobBuilder newInstance() {
        return new PushJobBuilder();
    }
}
